package com.ems.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ems.dao.MyDtoMapper;
import com.ems.entity.Exam;
import com.ems.exception.Result;
import com.ems.exception.ResultCode;
import com.ems.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    //1.20王
    @RequestMapping(value = "/examMsg",method = RequestMethod.GET)
    public Object exam(Integer stuid){
        List<Exam> exam = studentService.exam(stuid);
        //2008代表没有与你相关的考试
        if (exam == null) return Result.state(ResultCode.NULLEXAM);
        else return exam;
    }

    //1.22王
    @RequestMapping(value = "/score",method = RequestMethod.GET)
    public Object score(Integer stuid){
        return Result.ok().data(studentService.score(stuid));
    }

    @RequestMapping(value = "/getExamPaper",method = RequestMethod.GET)
    public Object getExamPaper(Integer eid, Integer uid){
        if(eid != null && uid != null)
        {
            return Result.ok().data(studentService.getExamPaper(eid, uid));
        }
        return Result.state(ResultCode.PARAM_ERROR);
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Object register(@RequestBody JSONObject json){
        if(json != null)
        {
            if(studentService.register(json)){
                return Result.ok();
            }
            return Result.no();
        }
        return Result.state(ResultCode.PARAM_ERROR);
    }


    @RequestMapping(value = "/putAnswer",method = RequestMethod.POST)
    public Object putAnswer(@RequestBody JSONObject json){
        if (json != null){
            if (studentService.putAnswer(json))
            {
                return Result.ok();
            }
            return Result.state(ResultCode.HASPUTANSWER);

        }
        return Result.state(ResultCode.PARAM_ERROR);
    }

}
